@file:JvmMultifileClass
@file:JvmName("ActivityKsl")


package hos.ksl

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.activity.result.ActivityResultLauncher
import androidx.core.app.ActivityCompat
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment

/**
 * <p>Title: ActivityExt </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @date : 2022/5/28 21:29
 * @version : 1.0
 */

fun Activity.hideInput() = window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)

/**
 * 禁止录屏和截图
 */
fun Activity.prohibitScreenRecord() {
    window.addFlags(WindowManager.LayoutParams.FLAG_SECURE);
}

/**
 * 设置录屏和截图
 */
fun Activity.clearScreenRecord() {
    window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
}


inline fun <reified T> Activity.intent(vararg params: Pair<String, Any?>, flag: Int = -1): Intent {
    val intent = Intent(this, T::class.java).apply {
        if (flag != -1) {
            this.addFlags(flag)
        }
        if (params.isNotEmpty()) putExtras(bundleOf(*params))
    }
    return intent
}

inline fun <reified T> Fragment.intent(vararg params: Pair<String, Any?>, flag: Int = -1): Intent {
    val intent = Intent(requireActivity(), T::class.java).apply {
        if (flag != -1) {
            this.addFlags(flag)
        }
        if (params.isNotEmpty()) putExtras(bundleOf(*params))
    }
    return intent
}

inline fun <reified T> Context.intent(vararg params: Pair<String, Any?>, flag: Int = -1): Intent {
    val intent = Intent(this, T::class.java).apply {
        if (flag != -1) {
            this.addFlags(flag)
        }
        if (this !is Activity) {
            this.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        }
        if (params.isNotEmpty()) putExtras(bundleOf(*params))
    }
    return intent
}

inline fun <reified T> View.intent(vararg params: Pair<String, Any?>, flag: Int = -1): Intent {
    val intent = Intent(context, T::class.java).apply {
        if (flag != -1) {
            this.addFlags(flag)
        }
        if (context !is Activity) {
            this.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        }
        if (params.isNotEmpty()) putExtras(bundleOf(*params))
    }
    return intent
}


inline fun <reified T> Activity.start(
    vararg params: Pair<String, Any?>,
    flag: Int = -1, requestCode: Int = -1,
    options: Bundle? = null
) {
    val intent = intent<T>(*params, flag = flag)
    if (requestCode == -1) {
        ActivityCompat.startActivity(this, intent, options)
    } else {
        ActivityCompat.startActivityForResult(this, intent, requestCode, options)
    }
}

inline fun <reified T> ActivityResultLauncher<Intent>.launch(
    activity: Activity,
    vararg params: Pair<String, Any?>,
    flag: Int = -1,
) {
    val intent = activity.intent<T>(*params, flag = flag)
    launch(intent)
}

inline fun <reified T> ActivityResultLauncher<Intent>.launch(
    fragment: Fragment,
    vararg params: Pair<String, Any?>,
    flag: Int = -1,
) {
    val intent = fragment.intent<T>(*params, flag = flag)
    launch(intent)
}


// 跳转到下一个Activity
inline fun <reified T> Fragment.start(
    vararg params: Pair<String, Any?>,
    flag: Int = -1,
    requestCode: Int = -1,
) {
    val intent = intent<T>(*params, flag = flag)
    if (requestCode == -1) {
        startActivity(intent)
    } else {
        @Suppress("DEPRECATION")
        startActivityForResult(intent, requestCode)
    }
}

inline fun <reified T> Context.start(
    vararg params: Pair<String, Any?>,
    flag: Int = -1,
) {
    startActivity(intent<T>(*params, flag = flag))
}


inline fun <reified T> View.start(
    vararg params: Pair<String, Any?>,
    flag: Int = -1, requestCode: Int = -1,
    options: Bundle? = null
) {
    if (requestCode == -1) {
        context.start<T>(*params, flag = flag)
    } else {
        (context as Activity).start<T>(
            *params,
            requestCode = requestCode,
            flag = flag,
            options = options
        )
    }
}

fun Activity.closeResult(vararg params: Pair<String, Any?>, resultCode: Int = -1) {
    val intent = Intent().apply {
        if (params.isNotEmpty()) putExtras(bundleOf(*params))
    }
    setResult(resultCode, intent)
    finish()
}

fun Fragment.closeResult(vararg params: Pair<String, Any?>, resultCode: Int = -1) {
    val intent = Intent().apply {
        if (params.isNotEmpty()) putExtras(bundleOf(*params))
    }
    requireActivity().setResult(resultCode, intent)
    requireActivity().finish()
}
